<?php
require_once 'data_form'.EXT;

class retirement extends data_form{

	function __construct(){
		parent::__construct();
		$this->load->helper('custom_string');
	}
	
	function index()
	{
		$this->data['module_title']	=	'Retirement';
		$row	=	null;
		$this->load->model(array('retirement_model','current_holding_model'));
		$this->load->model(array('pade_income_model','pade_e_loans_model','pade_e_dependants_model','explanation_model'));
		$this->load->model(array('existing_coverage_endowment_model','existing_coverage_term_model','existing_coverage_ilp_model','existing_coverage_wl_model','common_model'));
		
		$this->data['endowments']	=	$this->existing_coverage_endowment_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$this->data['terms']	=	$this->existing_coverage_term_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$this->data['ilps']	=	$this->existing_coverage_ilp_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$this->data['wls']	=	$this->existing_coverage_wl_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$row	=	$this->retirement_model->get_by(array('customer_id' => $_SESSION['customer']['id']));
		
		
		if($_SERVER["REQUEST_METHOD"] == "GET" && $row)
		{
			$data	=	array(
				'customer_id'						=>	$row->customer_id,
				'desired_monthly_income'			=>	$row->desired_monthly_income,
				'retirement_age'					=>	$row->retirement_age,
				'years_to_retirement'				=>	$row->	years_to_retirement,
				'monthly_income_at_retirement_age'	=>	$row->monthly_income_at_retirement_age,
				'number_of_years_to_receive'		=>	$row->number_of_years_to_receive,
				'inflation'							=>	$row->inflation,
				'explanation'						=>	$row->explanation
			);
			$_SESSION['data']	=	$data;
		}
		
		if($_SERVER["REQUEST_METHOD"]  == "POST")
		{
			$submit	=	$this->input->post('submit');
			if(!$submit)
			{
				$paramter	=	$this->data['paramter'];
				$desired_monthly_income	=	$_POST['textfield5'];
				$year_birth	=	date('Y',strtotime($_SESSION['customer']['date_of_birth']));
				$current_age	=	date('Y')-$year_birth;
				$re_age		=	($paramter ? $paramter->retirement_age : 60);
				$years_to_retirement	=	intval($re_age)-intval($current_age);
				$inflation	=	$this->input->post('inflation');
				
				$monthly_income	=	$desired_monthly_income*( pow(( 1+($inflation/100) ),($re_age-$current_age)) );
				$monthly_income	=	round($monthly_income);
				
				$data	=	array(
					'customer_id'						=>	$_SESSION['customer']['id'],
					'desired_monthly_income'			=>	$_POST['textfield5'],
					'retirement_age'					=>	$_POST['textfield'],
					'years_to_retirement'				=>	$years_to_retirement,
					'monthly_income_at_retirement_age'	=>	$monthly_income,
					'number_of_years_to_receive'		=>	$_POST['textfield6'],
					'inflation'							=>	$inflation,
					'explanation'						=>	($row ? $row->explanation : '')
					
				);
				$_SESSION['data']	=	$data;
			}
			else
			{
				$data	=	$_SESSION['data'];
				$data['explanation']	=	$_POST['explanation'];
				if($row){
					
					$this->retirement_model->update($row->id,$data);
				}
				else 
				{
					$this->retirement_model->insert($data);
				}
				echo "<script>window.close();</script>";
			}
			
			
		}
		$this->data['row']	=	(isset($_SESSION['data']) ? $_SESSION['data'] : null);
		$this->load->view("retirement/index", $this->data);
	}
	
	function monthly()
	{
		$paramter	=	$this->data['paramter'];
		$value	=	$this->input->post('value');
		$year_birth	=	date('Y',strtotime($_SESSION['customer']['date_of_birth']));
		$current_age	=	date('Y')-$year_birth;
		$re_age		=	$this->input->post('re_age');
		$inflation_rate =	$this->input->post('inflation');
		$result	=	$value*( pow(( 1+($inflation_rate/100) ),($re_age-$current_age)) );
		$result	=	round($result);
		die($result);
	} 

}